package cn.chia.front.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;

import cn.chia.front.dao.IIteminfoviewDAO;
import cn.chia.pojo.Iteminfoview;

@Component
public class IteminfoviewDAO extends HibernateDaoSupport implements IIteminfoviewDAO{
	private static final Logger log = LoggerFactory
			.getLogger(IteminfoviewDAO.class);
	@Autowired
	public IteminfoviewDAO(HibernateTemplate hibernateTemplate){
		super.setHibernateTemplate(hibernateTemplate);
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Iteminfoview> findByExample(Iteminfoview instance) {
		log.debug("finding Iteminfoview instance by example");
		try {
			List<Iteminfoview> results = getSession()
					.createCriteria("cn.chia.pojo.Iteminfoview")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List<Iteminfoview> findByProperty(String propertyName, Object value) {
		log.debug("finding Iteminfoview instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from Iteminfoview as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List<Iteminfoview> findByItemName(String propertyName, Object value) {
		log.debug("finding Iteminfoview instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from Iteminfoview as model where model."
					+ propertyName + " like ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, "%" + value + "%");
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	
	
	
	/**
	 * 查所有字段，根据需要只拿一部分数据
	 * @return
	 */
	public List<Iteminfoview> findAll() {
		log.debug("finding all Iteminfoview instances");
		try {
			String queryString = "from Iteminfoview";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	

}